package com.kfgj.hkp.service.house;

import com.kfgj.hkp.common.context.CurrentContext;
import com.kfgj.hkp.common.exception.BusinessException;
import com.kfgj.hkp.common.service.IServiceResponse;
import com.kfgj.hkp.common.service.SpringRestService;
import com.kfgj.hkp.common.user.entity.BaseUserEntity;
import com.kfgj.hkp.common.exception.RemoveStackBusinessException;
import com.kfgj.hkp.common.utils.ParamUtil;
import com.kfgj.hkp.common.utils.PhoneUtils;
import com.kfgj.hkp.entity.house.LinkCustomerEntity;
import com.kfgj.hkp.manager.house.business.ILinkCustomerManager;
import com.kfgj.hkp.service.house.vo.LinkCustomerServiceRequest;
import com.kfgj.hkp.service.house.vo.LinkCustomerServiceResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description
 * @Auth xpp
 * @Date 2019/11/19
 * @Version 1.0
 */
@RestController
@RequestMapping(path = "/linkCustomer")
public class LinkCustomerService extends SpringRestService {

    @Autowired
    private ILinkCustomerManager linkCustomerManager;

    @RequestMapping(path = "/create",
              method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
              produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> create(@RequestBody LinkCustomerServiceRequest request) throws IllegalAccessException {

        LinkCustomerServiceResponse response = new LinkCustomerServiceResponse();
        LinkCustomerEntity linkCustomerEntity = request.getEntity();
        if(null==linkCustomerEntity){
            throw new BusinessException("传入登记客户参数实体entity为:"+linkCustomerEntity);
        }

        // 根据业务需求对应字段校验
        List<String> list = new ArrayList();
        list.add("houseCode");
        //list.add("cusName");
        //list.add("cusPhone");
        //list.add("fixedPhone");
        //list.add("companyName");
        //
        //list.add("products");
        //list.add("needAcreage");
        //list.add("needPrice");
        //list.add("priceUnit");
        //list.add("contractEndTime");
        //
        //list.add("expireAccept");
        //list.add("description");
        //list.add("weChatSame");


        ParamUtil<LinkCustomerEntity> paramUtil = new ParamUtil<>();
        paramUtil.check(request.getEntity(),list);
        // 根据业务需求对应字段校验 end

        if(linkCustomerEntity.getCusPhone() != null){
            // 手机号格式
            if (! PhoneUtils.isMobile (linkCustomerEntity.getCusPhone())) {
                throw new RemoveStackBusinessException("客户手机号码格式错误");
            }
        }

        if(linkCustomerEntity.getCusPhone() != null  || linkCustomerEntity.getCusName() != null || linkCustomerEntity.getFixedPhone() != null || linkCustomerEntity.getCompanyName() != null ){
            int i = linkCustomerManager.create(linkCustomerEntity);
        }


        response.setResult(linkCustomerEntity);

        return this.success(response);
    }

    /**
     * 分页查询申请记录
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "查询申请记录",
            notes = "不分页")
    @RequestMapping(path = "/query",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> query(@RequestBody LinkCustomerServiceRequest request) throws IllegalAccessException {

        LinkCustomerServiceResponse response = new LinkCustomerServiceResponse();

        Map<String, Object> param = new HashMap<String, Object>();
        if(request.getEntity().getHouseCode()==null){throw new BusinessException("本接口houseCode字段需要传房源编码");}
        param.put("houseCode",request.getEntity().getHouseCode());
        param.put("isDeleted",false);
        List<LinkCustomerEntity> list = linkCustomerManager.findByMap(param);

        response.setResult(list);
        return this.success(response);
    }


    /**
     * 查询详情
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "查询详情",
            notes = "单个对象")
    @RequestMapping(path = "/detail",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> detail(@RequestBody LinkCustomerServiceRequest request) throws IllegalAccessException {

        LinkCustomerServiceResponse response = new LinkCustomerServiceResponse();

        Map<String, Object> param = new HashMap<String, Object>();
        if(request.getEntity().getLinkcusCode()==null){throw new BusinessException("要传linkcusCode字段,(登记客户编码)");}
        param.put("linkcusCode",request.getEntity().getLinkcusCode());
        param.put("isDeleted",false);
        List<LinkCustomerEntity> list = linkCustomerManager.findByMap(param);
        if(null==list){
            throw new BusinessException("未查询到详情,请传正确linkcusCode的值,传入值为:"+request.getEntity().getLinkcusCode());
        }
        if(list.size()==0){
            throw new BusinessException("0未查询到详情,请传正确linkcusCode的值,传入值为:"+request.getEntity().getLinkcusCode());
        }
        LinkCustomerEntity entity = list.get(0);

        response.setResult(entity);
        return this.success(response);
    }


    /**
     * 修改登记客户
     *
     * @param request
     * @return
     */
    @RequestMapping(path = "/update",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> update(@RequestBody LinkCustomerServiceRequest request) {

        LinkCustomerServiceResponse response = new LinkCustomerServiceResponse();

        Map<String, Object> param = new HashMap<String, Object>();
        if(request.getEntity().getLinkcusCode()==null){throw new BusinessException("要传linkcusCode字段,(登记客户编码)");}
        param.put("linkcusCode",request.getEntity().getLinkcusCode());
        param.put("isDeleted",false);
        List<LinkCustomerEntity> list = linkCustomerManager.findByMap(param);
        if(null==list){
            throw new BusinessException("未查询到详情,请传正确linkcusCode的值,传入值为:"+request.getEntity().getLinkcusCode());
        }
        if(list.size()==0){
            throw new BusinessException("0未查询到详情,请传正确linkcusCode的值,传入值为:"+request.getEntity().getLinkcusCode());
        }

        if(request.getEntity().getCusPhone() != null){
            // 手机号格式
            if (! PhoneUtils.isMobile (request.getEntity().getCusPhone())) {
                throw new RemoveStackBusinessException("客户手机号码格式错误");
            }
        }
        BaseUserEntity userInfo = CurrentContext.getUserInfo();
        LinkCustomerEntity lkdbEntity = list.get(0);
        LinkCustomerEntity newEntity = request.getEntity();
        newEntity.setId(lkdbEntity.getId());
        newEntity.setLastUpdateCode(userInfo.getUserCode());
        newEntity.setLastUpdateName(userInfo.getUserName());
        newEntity.setLastUpdateTime(new Date());

        if (null != request.getEntity()) {
            linkCustomerManager.update(newEntity);
        }

        return this.success(response);
    }



    /**
     * 删除登记客户
     *
     * @param request
     * @return
     */
    @RequestMapping(path = "/delete",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> delete(@RequestBody LinkCustomerServiceRequest request) {

        LinkCustomerServiceResponse response = new LinkCustomerServiceResponse();

        Map<String, Object> param = new HashMap<String, Object>();
        if(request.getEntity().getLinkcusCode()==null){throw new BusinessException("要传linkcusCode字段,(登记客户编码)");}
        param.put("linkcusCode",request.getEntity().getLinkcusCode());
        param.put("isDeleted",false);
        List<LinkCustomerEntity> list = linkCustomerManager.findByMap(param);
        if(null==list){
            throw new BusinessException("未查询到详情,请传正确linkcusCode的值,传入值为:"+request.getEntity().getLinkcusCode());
        }
        if(list.size()==0){
            throw new BusinessException("0未查询到详情,请传正确linkcusCode的值,传入值为:"+request.getEntity().getLinkcusCode());
        }
        BaseUserEntity userInfo = CurrentContext.getUserInfo();
        LinkCustomerEntity lkdbEntity = list.get(0);
        LinkCustomerEntity inEntity = request.getEntity();
        LinkCustomerEntity newEntity = new LinkCustomerEntity();
        newEntity.setLinkcusCode(inEntity.getLinkcusCode());
        newEntity.setId(lkdbEntity.getId());
        newEntity.setIsDeleted(true);
        newEntity.setLastUpdateCode(userInfo.getUserCode());
        newEntity.setLastUpdateName(userInfo.getUserName());
        newEntity.setLastUpdateTime(new Date());

        if (null != request.getEntity()) {
            linkCustomerManager.update(newEntity);
        }

        return this.success(response);
    }
//end
}


